#include "../src/SQLAction.hpp"
#include <iostream>
#include <list>
#include <spdlog/spdlog.h>

int main()
{
  spdlog::set_level(spdlog::level::debug);

  SQLAction db("hbty","SUSE8241","192.168.1.11","hbty");

  std::cout<<">>>>>>>>> Test Student Item"<<std::endl;
  db.CreateStudent(10,"libpqxx1");
  db.CreateStudent(11,"libpqxx2");
  db.CreateStudent(12,"libpqxx3");
  db.CreateStudent(13,"libpqxx4");
  db.CreateStudent(14,"libpqxx5");
  auto val=db.GetStudentInf();
  for(const auto & i : val)
    std::cout<<"ID: "<<i.ID<<" Name: "<<i.Name<<std::endl;
  for(int i=10;i<=14;i++)
    db.DeleteStudent(i);

  std::cout<<">>>>>>>>> Test Task Item"<<std::endl;
  std::list<long> testTaskIDList;
  for(int i=0;i<5;i++)
    testTaskIDList.push_back(db.CreateTask("testpqxx"));
  auto valTask=db.GetTaskInf();
  for(auto & i : valTask)
    std::cout<<"TaskID: "<<i.id<<" Name: "<<i.name<<" CreateTime: "<<i.createTime<<std::endl;
  for(auto & i : testTaskIDList)
  {
    try
    {
      db.DeleteTask(i);
    }
    catch(const std::exception& e)
    {
      std::cout<<"Delete Failed ,Continue "<<e.what()<<std::endl;
    }
  }

  db.CreateStudent(10,"RecordTest");
  long testRecordTask=db.CreateTask("RecordTest");
  long testRecordID=db.CreateRecord(10,testRecordTask);
  db.RecordFinish(testRecordID);
  auto RecordList=db.GetRecordIDList(1);
  std::cout<<">>>>>>>>> All Record Item"<<std::endl;
  for(auto & i : RecordList)
  {
    auto inf=db.GetRecordInf(i);
    std::cout
      <<"RecID: "<<i
      <<" stuID: "<<std::get<0>(inf)
      <<" taskID: "<<std::get<1>(inf)
      <<" createTime: "<<std::get<2>(inf)
      <<" smplStatus: "<<std::get<3>(inf)
      <<" recordStatus: "<<std::get<4>(inf)
      <<std::endl;
  }
  std::cout<<">>>>>>>>> Handle Finish Item"<<std::endl;
  RecordList=db.GetRecordIDList(1);
  for(auto & i : RecordList)
  {
    auto inf=db.GetRecordInf(i);
    std::cout
      <<"RecID: "<<i
      <<" stuID: "<<std::get<0>(inf)
      <<" taskID: "<<std::get<1>(inf)
      <<" createTime: "<<std::get<2>(inf)
      <<" smplStatus: "<<std::get<3>(inf)
      <<" recordStatus: "<<std::get<4>(inf)
      <<std::endl;
  }
  std::cout<<">>>>>>>>> Unhandle Item"<<std::endl;
  RecordList=db.GetUnhandleIDList();
  for(auto & i : RecordList)
  {
    auto inf=db.GetRecordInf(i);
    std::cout
      <<"RecID: "<<i
      <<" stuID: "<<std::get<0>(inf)
      <<" taskID: "<<std::get<1>(inf)
      <<" createTime: "<<std::get<2>(inf)
      <<" smplStatus: "<<std::get<3>(inf)
      <<" recordStatus: "<<std::get<4>(inf)
      <<std::endl;
  }
  db.DeleteRecord(testRecordID);
  db.DeleteTask(testRecordTask);
  db.DeleteStudent(10);
}
